<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form >
                                                            <!--@blur：失去焦点事件-->
    <input type="text" name="username"  placeholder="用户名" @blur="check()"> {{info}}<br>
    <input type="text" name="password"  placeholder="密码"><br>
    <input type="button" value="注册" @click="reg()"><br>
</form>
<!--引入axios和vue-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.js"></script>
<script>
    let v = new Vue({
        el:"form",
        data:{
            info:""
        },
        methods:{
            check(){
                //获取表单数据
                let formdata = new FormData(document.querySelector("form"));
                //发起异步post请求
                axios.post("/check",formdata).then(function (response) {
                    if(response.data==1){
                        v.info="可用";
                    }else {
                        v.info="不可用";
                    }
                })
            },
            reg(){
                //获取表单数据，  以下这种写法，会把表单的数据自动封装到formData对象中
               let formData =  new FormData(document.querySelector("form"));
               //发起异步请求
               axios.post("/reg",formData).then(function (response) {
                   alert(response.data);
               })
            }
        }
    })
</script>

</body>
</html>